<template>
    <div class="system-param-config-container">
        <el-form :inline="true" ref="form" class="system-search-form" :model="searchForm">
            <el-form-item>
                <el-cascader
                    class="go-out"
                    v-model="searchForm.mechanism"
                    :options="mechanismList"
                    :show-all-levels="false"
                    :props="{ checkStrictly: true, emitPath: false }"
                    filterable
                    size="small">
                </el-cascader>
            </el-form-item>
            <el-form-item>
                <el-button class="sub-btn" type="primary" size="small" @click="getData">查询</el-button>
            </el-form-item>
        </el-form>
        <div class="table-box">
            <el-table
                :data="tableData"
                tooltip-effect="light"
                border
                :max-height="tableHeight"
                style="width: 100%">
                <el-table-column
                    label="序号"
                    align="center"
                    type="index"
                    width="50">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="参数名称"
                    width="260">
                </el-table-column>
                <el-table-column
                    prop="value"
                    label="参数">
                    <template slot-scope="scope">
                        <div v-if="scope.row.name === 'DRG测算模式与费率'" class="table-input">
                            <div>
                                测算模式:
                                <el-radio-group v-model="scope.row.value.测算模式" @change="changeRadio">
                                    <el-radio label="CHS-DRG分组与付费">CHS-DRG分组与付费</el-radio>
                                    <el-radio label="DRGs病组点数法">DRGs病组点数法</el-radio>
                                </el-radio-group>
                            </div>
                            <div style="margin-bottom: 4px;margin-top: 8px">
                                费&emsp;&emsp;率:
                                <el-radio-group v-model="scope.row.value.费率方式">
                                    <el-radio label="通用费率">通用费率</el-radio>
                                    <el-radio v-show="scope.row.value.测算模式 === 'CHS-DRG分组与付费'" label="按支付方式费率">按支付方式费率</el-radio>
                                </el-radio-group>
                            </div>
                            <div v-show="scope.row.value.费率方式 === '通用费率'" style="display: inline-block">
                                <span>通用费率:</span><el-input size="mini" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" v-model="scope.row.value.通用费率"></el-input>
                                <span v-show="scope.row.value.测算模式 === 'DRGs病组点数法'">基准点数:</span><el-input v-show="scope.row.value.测算模式 === 'DRGs病组点数法'" size="mini" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" v-model="scope.row.value.基准点数"></el-input>
                            </div>
                            <div v-show="scope.row.value.费率方式 === '按支付方式费率'" style="display: inline-block">
                                <span>城镇职工:</span><el-input size="mini" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" v-model="scope.row.value.城镇职工"></el-input>
                                <span>城乡居民:</span><el-input onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" size="mini" v-model="scope.row.value.城乡居民"></el-input>
                            </div>
                        </div>
                        <div v-else class="table-input">
                            <span>低倍率:</span><el-input size="mini" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" v-model="scope.row.value.低倍率"></el-input>
                            <span>高倍率:</span><el-input size="mini" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" v-model="scope.row.value.高倍率"></el-input>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="160"
                    prop="modifyTime"
                    align="center"
                    label="更新时间">
                </el-table-column>
                <el-table-column
                    width="120"
                    align="center"
                    label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" v-auth="{code: 300005003001}" @click="clickSave(scope.$index, scope.row)">保存</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
    import {getParamsDataMenu, saveParamsData} from '@/api/system/param-config'

    export default {
        name: 'index',
        data () {
            return {
                // 搜索表单
                searchForm: {
                    mechanism: ''
                },
                // 机构列表
                mechanismList: [],
                // 表格数据
                tableData: [],
                // 表格高度
                tableHeight: 100,
                // 使用储存的数据，不去请求
                useStore: false
            }
        },
        created () {
            // tags判断入口是否从选项卡进入
            this.useStore = this.$route.query.tags === 'tags'
            let height = document.documentElement.clientHeight - 213 - 10 - 78 - 5
            this.tableHeight = height > 107 ? height : 107
            this.getMechanismList()
            this.initData()
            this.getData()
        },
        mounted () {
            window.onresize = () => {
                // 213=其他元素高度60+50+48+55 10=距离底部边距 78=分页组件高度 5=适应性调整
                let height = document.documentElement.clientHeight - 213 - 10 - 78 - 5
                this.tableHeight = height > 107 ? height : 107
            }
        },
        methods: {
            /**
             * 初始化页面参数
             */
            initData () {
                let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                if (JSON.stringify(query) !== '{}') {
                    this.searchForm.mechanism = query.orgId
                }
            },
            /**
             * 修改测算模式
             */
            changeRadio (val) {
                if (val === 'DRGs病组点数法') {
                    this.tableData[0].value.费率方式 = '通用费率'
                }
            },
            /**
             * 获取机构列表
             */
            getMechanismList () {
                let mechanismList = this.getLocalStorage('userInfo', 'mechanism')
                let parentList = []
                mechanismList.forEach(item => {
                    // 一级机构
                    if (!item.pid) {
                        parentList.push({
                            value: item.id,
                            label: item.name,
                            children: []
                        })
                        // 有一级机构的二级机构
                        mechanismList.forEach(it => {
                            if (it.pid === item.id) {
                                parentList[parentList.length - 1].children.push({
                                    value: it.id,
                                    label: it.name,
                                    children: null
                                })
                            }
                        })
                        if (!parentList[parentList.length - 1].children.length) {
                            parentList[parentList.length - 1].children = null
                        }
                    } else {
                        // 二级机构
                        let parent = mechanismList.filter(i => item.pid === i.id)
                        if (!parent.length) {
                            parentList.push({
                                value: item.id,
                                label: item.name,
                                children: null
                            })
                        }
                    }
                })
                this.mechanismList = parentList
                if (this.mechanismList.length) {
                    if (this.getLocalStorage('userMechanism', 'id') && this.hasMechanismId(this.getLocalStorage('userMechanism', 'id'))) {
                        this.searchForm.mechanism = this.getLocalStorage('userMechanism', 'id')
                    } else {
                        this.searchForm.mechanism = parentList[0].value
                    }
                }
            },
            /**
             * 判断所属机构是否在组织机构中
             * @param id 所属机构id
             */
            hasMechanismId (id) {
                let res = this.mechanismList.filter(item => item.value === id)
                return res.length !== 0
            },
            /**
             * 获取表格数据
             */
            getData () {
                let params = {
                    orgId: this.searchForm.mechanism
                }
                this.setGetStore(this.$route.name, params)
                if (this.getSession(this.$route.name, 'getParamsDataMenu') && this.useStore) {
                    this.useStore = false
                    let res = this.getSession(this.$route.name, 'getParamsDataMenu')
                    if (res.code.toString() === '200') {
                        this.tableData = [
                            {
                                orgId: this.searchForm.mechanism,
                                name: 'DRG测算模式与费率',
                                value: {
                                    测算模式: '',
                                    费率方式: '',
                                    通用费率: '',
                                    基准点数: '',
                                    城镇职工: '',
                                    城乡居民: ''
                                },
                                modifyTime: ''
                            }, {
                                orgId: this.searchForm.mechanism,
                                name: '高低费率',
                                value: {
                                    低倍率: '',
                                    高倍率: ''
                                },
                                modifyTime: ''
                            }
                        ]
                        res.data.forEach(item => {
                            switch (item.name) {
                            case 'DRG测算模式与费率':
                                this.tableData[0].value = JSON.parse(item.value)
                                this.tableData[0].modifyTime = item.modifyTime
                                break
                            case '高低费率':
                                this.tableData[1].value = JSON.parse(item.value)
                                this.tableData[1].modifyTime = item.modifyTime
                                break
                            }
                        })
                    }
                } else {
                    getParamsDataMenu(params.orgId).then(res => {
                        this.useStore = false
                        this.setSession(this.$route.name, {getParamsDataMenu: res})
                        if (res.code.toString() === '200') {
                            this.tableData = [
                                {
                                    orgId: this.searchForm.mechanism,
                                    name: 'DRG测算模式与费率',
                                    value: {
                                        测算模式: '',
                                        费率方式: '',
                                        通用费率: '',
                                        基准点数: '',
                                        城镇职工: '',
                                        城乡居民: ''
                                    },
                                    modifyTime: ''
                                }, {
                                    orgId: this.searchForm.mechanism,
                                    name: '高低费率',
                                    value: {
                                        低倍率: '',
                                        高倍率: ''
                                    },
                                    modifyTime: ''
                                }
                            ]
                            res.data.forEach(item => {
                                switch (item.name) {
                                case 'DRG测算模式与费率':
                                    this.tableData[0].value = JSON.parse(item.value)
                                    this.tableData[0].modifyTime = item.modifyTime
                                    break
                                case '高低费率':
                                    this.tableData[1].value = JSON.parse(item.value)
                                    this.tableData[1].modifyTime = item.modifyTime
                                    break
                                }
                            })
                        } else {
                            this.$message.error(res.message)
                        }
                    })
                }
            },
            clickSave (index, row) {
                if (row.name === 'DRG测算模式与费率') {
                    let text = `修改"测算模式"将对测算结果产生重大影响！您确定修改并保存"测算模式"为"${row.value.测算模式}"吗?`
                    let title = '确认保存'
                    this.$confirm(text, title, {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        center: true
                    }).then(() => {
                        this.saveData(index, row)
                    }).catch()
                } else {
                    this.saveData(index, row)
                }
            },
            /**
             * 保存修改
             * @param index 索引
             * @param row 行数据
             */
            saveData (index, row) {
                let params = {
                    orgId: row.orgId || this.searchForm.mechanism,
                    name: row.name,
                    value: row.value
                }
                if (params.name === 'DRG测算模式与费率') {
                    if (params.value.费率方式 === '通用费率') {
                        if (!params.value.通用费率 || params.value.通用费率 === '0' || isNaN(Number(params.value.通用费率)) || Number(params.value.通用费率) < 0) {
                            this.$message.error('通用费率请填写大于0的数字')
                            return false
                        } else if (params.value.测算模式 === 'DRGs病组点数法') {
                            if (!params.value.基准点数 || params.value.基准点数 === '0' || isNaN(Number(params.value.基准点数)) || Number(params.value.基准点数) < 0) {
                                this.$message.error('基准点数请填写大于0的数字')
                                return false
                            }
                        }
                    } else if (params.value.费率方式 === '按支付方式费率') {
                        if (!params.value.城镇职工 || params.value.城镇职工 === '0' || isNaN(Number(params.value.城镇职工)) || Number(params.value.城镇职工) < 0) {
                            this.$message.error('城镇职工请填写大于0的数字')
                            return false
                        } else if (!params.value.城乡居民 || params.value.城乡居民 === '0' || isNaN(Number(params.value.城乡居民)) || Number(params.value.城乡居民) < 0) {
                            this.$message.error('城乡居民请填写大于0的数字')
                            return false
                        }
                    }
                    params.value.通用费率 = Number(params.value.通用费率)
                    params.value.基准点数 = Number(params.value.基准点数)
                    params.value.城镇职工 = Number(params.value.城镇职工)
                    params.value.城乡居民 = Number(params.value.城乡居民)
                }
                if (params.name === '高低费率') {
                    if (!params.value.低倍率 || params.value.低倍率 === '0' || isNaN(Number(params.value.低倍率)) || Number(params.value.低倍率) < 0) {
                        this.$message.error('低倍率请填写大于0的数字')
                        return false
                    } else if (!params.value.高倍率 || params.value.高倍率 === '0' || isNaN(Number(params.value.高倍率)) || Number(params.value.高倍率) < 0) {
                        this.$message.error('高倍率请填写大于0的数字')
                        return false
                    }
                    params.value.低倍率 = Number(params.value.低倍率)
                    params.value.高倍率 = Number(params.value.高倍率)
                }
                if (['DRG测算模式与费率', '高低费率'].includes(params.name)) {
                    params.value = JSON.stringify(params.value)
                }
                saveParamsData(params).then(res => {
                    if (res.code.toString() === '200') {
                        this.$message.success('保存成功')
                        this.getData()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            }
        },
        beforeDestroy () {
            window.onresize = null
        }
    }
</script>

<style lang="scss" scoped>
    .system-param-config-container {
        .system-search-form {
            .el-form-item {
                margin-bottom: 0;
            }
        }
        .table-box {
            background: #fff;
            padding: 10px;
            box-sizing: border-box;
            height: calc(100vh - 183px);
            .add-btn {
                margin-bottom: 10px;
            }
            .table-input {
                .el-input {
                    width: 100px;
                    margin-right: 10px;
                }
            }
        }
    }
</style>
